package com.pay.ioopos.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.pay.ioopos.BindState;
import com.pay.ioopos.IconFontFactory;
import com.pay.ioopos.R;
import com.pay.ioopos.ScanListener;
import com.pay.ioopos.SubScreenBackgroudLoader;
import com.pay.ioopos.SubScreenFactory;
import com.pay.ioopos.support.scan.ScanIcCard;
import com.pay.ioopos.support.scan.ScanLife;
import com.pay.ioopos.support.scan.ScanQrCode;

public class PayScanFragment extends AbstractNetworkFragment implements BindState {
    private String amount;
    private ScanListener listener;

    public PayScanFragment(String amount) {
        //super(BarcodeFormat.QR_CODE);
        this.amount = amount;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tip_horizontal, container, false);
        TextView iconView = view.findViewById(R.id.tip_icon);
        iconView.setTypeface(IconFontFactory.getHalflings(getActivity()));
        TextView msgView = view.findViewById(R.id.tip_msg);
        msgView.setText(R.string.pay_wait);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        ScanLife life = new ScanLife(new ScanQrCode(), new ScanIcCard());
        life.setScanListener(listener);
        life.bindToLifecycle(this);
    }

    @Override
    public boolean useNetwork() {
        return true;
    }

    @Override
    protected void execute() throws Exception {
        speak("请支付" + amount + "元");
        SubScreenFactory.startSubScreen(this::flushSubScreenToShowMoney);
        /*
        try {
            FragmentManager fragmentManager = getChildFragmentManager();
            if (fragmentManager.isStateSaved()) {
                return;
            }
            TipHorizontalFragment tip = new TipHorizontalFragment(TipFragment.TipType.WAIT, R.string.pay_wait);
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.disallowAddToBackStack();
            fragmentTransaction.replace(R.id.scan, tip);
            fragmentTransaction.commit();
            speak("请支付" + amount + "元");
            SubScreenFactory.startSubScreen(this::flushSubScreenToShowMoney);
        } catch (Exception e) {

        }
        */
    }

    public void setListener(ScanListener listener) {
        this.listener = listener;
    }


    private void flushSubScreenToShowMoney() {

        Canvas canvas;
        Bitmap bitmap = SubScreenBackgroudLoader.getInstance().getPayInitBitmap();
        if (null == bitmap) {
            bitmap = SubScreenFactory.createBitmap();
            canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
        } else {
            bitmap = SubScreenFactory.scaledBitmap(bitmap);
            canvas = new Canvas(bitmap);
        }

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(25f);

        String text = "支付金额(元)";
        int fontWidth = (int) paint.measureText(text);
        int startX = (bitmap.getWidth() - fontWidth) / 2;
        canvas.drawText(text, startX, 130, paint);

        paint.setTextSize(80f);
        text = amount;
        fontWidth = (int) paint.measureText(text);
        startX = (bitmap.getWidth() - fontWidth) / 2;
        canvas.drawText(text, startX, 220, paint);

        canvas.save();
        canvas.restore();

        SubScreenFactory.flush(bitmap);

    }

}
